package net.sk.china.front.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sk.china.common.model.user.UserAddress;
import net.sk.china.common.request.PageParamRequest;
import net.sk.china.common.response.CommonPage;
import net.sk.china.common.response.CommonResult;
import net.sk.china.front.request.user.UserAddressRequest;
import net.sk.china.front.service.user.UserAddressService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * +----------------------------------------------------------------------
 * | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2018-2024 聊来All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">apache</a> )
 * +----------------------------------------------------------------------
 * | @Author: 聊来 <18970881148@qq.com>
 * +----------------------------------------------------------------------
 * | DateTime: 2024/6/13 21:04
 * +----------------------------------------------------------------------
 */

@RestController
@RequestMapping("address/")
@Api(tags = "收货地址")
public class UserAddressController {

    private final UserAddressService userAddressService;

    public UserAddressController(UserAddressService userAddressService) {
        this.userAddressService = userAddressService;
    }

    @ApiOperation(value = "列表")
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public CommonResult<CommonPage<UserAddress>> getList(PageParamRequest page) {
        return CommonResult.success(CommonPage.restPage(userAddressService.getList(page)));
    }

    @ApiOperation(value = "保存")
    @RequestMapping(value = "create", method = RequestMethod.POST)
    public CommonResult<UserAddress> create(@RequestBody @Validated UserAddressRequest request) {
        return CommonResult.success(userAddressService.create(request));
    }

    @ApiOperation(value = "删除")
    @RequestMapping(value = "delete/{id}", method = RequestMethod.POST)
    public CommonResult<String> delete(@PathVariable(value = "id") Integer id) {
        if (userAddressService.delete(id)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation(value = "地址详情")
    @RequestMapping(value = "detail/{id}", method = RequestMethod.GET)
    public CommonResult<UserAddress> getDetail(@PathVariable("id") Integer id) {
        return CommonResult.success(userAddressService.getDetail(id));
    }

    @ApiOperation(value = "获取默认地址")
    @RequestMapping(value = "default", method = RequestMethod.GET)
    public CommonResult<UserAddress> getDefault() {
        return CommonResult.success(userAddressService.getDefault());

    }

    @ApiOperation(value = "设置默认地址")
    @RequestMapping(value = "default/{id}", method = RequestMethod.POST)
    public CommonResult<UserAddress> setDefault(@PathVariable("id") Integer id) {
        if (userAddressService.setDefault(id)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }
}
